home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / unarj.exe / ENVIRON.C < prev    next >
C/C++ Source or Header  |  1991-09-06  |  14KB  |  742 lines

  1. /* ENVIRON.C, UNARJ, R JUNG, 09/01/91
  2.  * Implementation dependent routines
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   The UNIX file date-time stamping code is derived from ZOO by
  18.  *   Rahul Dhesi.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/09/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler    Portabilized.
  24.  * 04/29/91  R. Jung     Added get_mode_str().
  25.  * 05/08/91  R. Jung     Combined set_ftime() and set_fmode().
  26.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  27.  *                       set_ftime_mode().
  28.  * 07/07/91  R. Jung     Added default_case_path() and UNIX section.
  29.  * 07/24/91  R. Jung     Fixed use of _chmod to handle directories.
  30.  * 08/27/91  R. Jung     Added date/time handling to Coherent.
  31.  * 09/01/91  R. Jung     Added #include <stdlib.h> to vanilla section.
  32.  *                       Added file date-time stamping to UNIX section.
  33.  *
  34.  */
  35.  
  36. #include "unarj.h"
  37.  
  38. #ifdef __TURBOC__
  39.  
  40. #define SUBS_DEFINED
  41.  
  42. #include <string.h>
  43. #include <dos.h>
  44. #include <io.h>
  45. #include <fcntl.h>
  46. #include <alloc.h>
  47.  
  48. FILE *
  49. file_open(name, mode)
  50. char *name;
  51. char *mode;
  52. {
  53.     return fopen(name, mode);
  54. }
  55.  
  56. int
  57. file_read(buf, size, nitems, stream)
  58. char *buf;
  59. int  size;
  60. int  nitems;
  61. FILE *stream;
  62. {
  63.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  64. }
  65.  
  66. int
  67. file_seek(stream, offset, mode)
  68. FILE *stream;
  69. long offset;
  70. int  mode;
  71. {
  72.     return fseek(stream, offset, mode);
  73. }
  74.  
  75. long
  76. file_tell(stream)
  77. FILE *stream;
  78. {
  79.     return ftell(stream);
  80. }
  81.  
  82. int
  83. file_write(buf, size, nitems, stream)
  84. char *buf;
  85. int  size;
  86. int  nitems;
  87. FILE *stream;
  88. {
  89.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  90. }
  91.  
  92. voidp *
  93. xmalloc(size)
  94. int size;
  95. {
  96.     return (voidp *)malloc((size_t) size);
  97. }
  98.  
  99. void
  100. case_path(name)
  101. char *name;
  102. {
  103.     strupper(name);
  104. }
  105.  
  106. void
  107. default_case_path(name)
  108. char *name;
  109. {
  110.     strupper(name);
  111. }
  112.  
  113. int
  114. file_exists(name)
  115. char *name;
  116. {
  117.     return (access(name, 0) == 0);
  118. }
  119.  
  120. void
  121. get_mode_str(str, mode)
  122. char *str;
  123. uint mode;
  124. {
  125.     strcpy(str, "---W");
  126.     if (mode & FA_ARCH)
  127.         str[0] = 'A';
  128.     if (mode & FA_SYSTEM)
  129.         str[1] = 'S';
  130.     if (mode & FA_HIDDEN)
  131.         str[2] = 'H';
  132.     if (mode & FA_RDONLY)
  133.         str[3] = 'R';
  134. }
  135.  
  136. int
  137. set_ftime_mode(name, tstamp, attribute, host)
  138. char  *name;
  139. ulong tstamp;
  140. uint  attribute;
  141. uint  host;
  142. {
  143.     FILE *fd;
  144.     int code;
  145.  
  146.     if ((fd = fopen(name, "r+b")) == NULL)
  147.         return -1;
  148.     code = setftime(fileno(fd), (struct ftime *) &tstamp);
  149.     fclose(fd);
  150.     if (host == OS)
  151.     {
  152.         attribute &= 0x27;
  153.         if (_chmod(name, 1, attribute) == -1)
  154.             return -1;
  155.     }
  156.     return code;
  157. }
  158.  
  159. #endif
  160.  
  161. #ifdef _QC
  162.  
  163. #define SUBS_DEFINED
  164.  
  165. #include <string.h>
  166. #include <dos.h>
  167. #include <io.h>
  168. #include <fcntl.h>
  169. #include <malloc.h>
  170.  
  171. FILE *
  172. file_open(name, mode)
  173. char *name;
  174. char *mode;
  175. {
  176.     return fopen(name, mode);
  177. }
  178.  
  179. int
  180. file_read(buf, size, nitems, stream)
  181. char *buf;
  182. int  size;
  183. int  nitems;
  184. FILE *stream;
  185. {
  186.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  187. }
  188.  
  189. int
  190. file_seek(stream, offset, mode)
  191. FILE *stream;
  192. long offset;
  193. int  mode;
  194. {
  195.     return fseek(stream, offset, mode);
  196. }
  197.  
  198. long
  199. file_tell(stream)
  200. FILE *stream;
  201. {
  202.     return ftell(stream);
  203. }
  204.  
  205. int
  206. file_write(buf, size, nitems, stream)
  207. char *buf;
  208. int  size;
  209. int  nitems;
  210. FILE *stream;
  211. {
  212.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  213. }
  214.  
  215. voidp *
  216. xmalloc(size)
  217. int size;
  218. {
  219.     return (voidp *)malloc((size_t) size);
  220. }
  221.  
  222. void
  223. case_path(name)
  224. char *name;
  225. {
  226.     strupper(name);
  227. }
  228.  
  229. void
  230. default_case_path(name)
  231. char *name;
  232. {
  233.     strupper(name);
  234. }
  235.  
  236. int
  237. file_exists(name)
  238. char *name;
  239. {
  240.     return (access(name, 0) == 0);
  241. }
  242.  
  243. void
  244. get_mode_str(str, mode)
  245. char *str;
  246. uint mode;
  247. {
  248.     strcpy(str, "---W");
  249.     if (mode & FA_ARCH)
  250.         str[0] = 'A';
  251.     if (mode & FA_SYSTEM)
  252.         str[1] = 'S';
  253.     if (mode & FA_HIDDEN)
  254.         str[2] = 'H';
  255.     if (mode & FA_RDONLY)
  256.         str[3] = 'R';
  257. }
  258.  
  259. int
  260. set_ftime_mode(name, tstamp, attribute, host)
  261. char  *name;
  262. ulong tstamp;
  263. uint  attribute;
  264. uint  host;
  265. {
  266.     FILE *fd;
  267.     int code;
  268.     uint date_stamp, time_stamp;
  269.  
  270.     date_stamp = (uint)(tstamp >> 16);
  271.     time_stamp = (uint)(tstamp & 0xFFFF);
  272.     if ((fd = fopen(name, "r+b")) == NULL)
  273.         return -1;
  274.     code = _dos_setftime(fileno(fd), date_stamp, time_stamp);
  275.     fclose(fd);
  276.     if (host == OS)
  277.     {
  278.         if (_dos_setfileattr(name, attribute))
  279.             return -1;
  280.     }
  281.     return code;
  282. }
  283.  
  284. #endif
  285.  
  286. #ifdef _OS2
  287.  
  288. #define SUBS_DEFINED
  289.  
  290. #include <string.h>
  291. #define INCL_DOSFILEMGR
  292. #include <os2.h>
  293. #include <io.h>
  294. #include <fcntl.h>
  295.  
  296. FILE *
  297. file_open(name, mode)
  298. char *name;
  299. char *mode;
  300. {
  301.     return fopen(name, mode);
  302. }
  303.  
  304. int
  305. file_read(buf, size, nitems, stream)
  306. char *buf;
  307. int  size;
  308. int  nitems;
  309. FILE *stream;
  310. {
  311.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  312. }
  313.  
  314. int
  315. file_seek(stream, offset, mode)
  316. FILE *stream;
  317. long offset;
  318. int  mode;
  319. {
  320.     return fseek(stream, offset, mode);
  321. }
  322.  
  323. long
  324. file_tell(stream)
  325. FILE *stream;
  326. {
  327.     return ftell(stream);
  328. }
  329.  
  330. int
  331. file_write(buf, size, nitems, stream)
  332. char *buf;
  333. int  size;
  334. int  nitems;
  335. FILE *stream;
  336. {
  337.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  338. }
  339.  
  340. voidp *
  341. xmalloc(size)
  342. int size;
  343. {
  344.     return (voidp *)malloc((size_t) size);
  345. }
  346.  
  347. void
  348. case_path(name)
  349. char *name;
  350. {
  351.     strupper(name);
  352. }
  353.  
  354. void
  355. default_case_path(name)
  356. char *name;
  357. {
  358.     strupper(name);
  359. }
  360.  
  361. int
  362. file_exists(name)
  363. char *name;
  364. {
  365.     return (access(name, 0) == 0);
  366. }
  367.  
  368. void
  369. get_mode_str(str, mode)
  370. char *str;
  371. uint mode;
  372. {
  373.     strcpy(str, "---W");
  374.     if (mode & FA_ARCH)
  375.         str[0] = 'A';
  376.     if (mode & FA_SYSTEM)
  377.         str[1] = 'S';
  378.     if (mode & FA_HIDDEN)
  379.         str[2] = 'H';
  380.     if (mode & FA_RDONLY)
  381.         str[3] = 'R';
  382. }
  383.  
  384. int
  385. set_ftime_mode(name, tstamp, attribute, host)
  386. char  *name;
  387. ulong tstamp;
  388. uint  attribute;
  389. uint  host;
  390. {
  391.     int code;
  392.     FDATE date_stamp;
  393.     FTIME time_stamp;
  394.     HFILE handle;
  395.     FILESTATUS info;
  396.     USHORT action;
  397.  
  398.     date_stamp.day = ts_day (tstamp);
  399.     date_stamp.month = ts_month (tstamp);
  400.     date_stamp.year = ts_year (tstamp) - 1980;
  401.     time_stamp.twosecs = ts_sec (tstamp) / 2;
  402.     time_stamp.minutes = ts_min (tstamp);
  403.     time_stamp.hours = ts_hour (tstamp);
  404.     if (DosOpen (name, &handle, &action, 0L, 0, FILE_OPEN,
  405.                  OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L) != 0)
  406.         return -1;
  407.     info.fdateCreation = date_stamp;
  408.     info.ftimeCreation = time_stamp;
  409.     info.fdateLastAccess = date_stamp;
  410.     info.ftimeLastAccess = time_stamp;
  411.     info.fdateLastWrite = date_stamp;
  412.     info.ftimeLastWrite = time_stamp;
  413.     info.cbFile = 0;
  414.     info.cbFileAlloc = 0;
  415.     info.attrFile = 0;
  416.     code = (int)DosSetFileInfo (handle, 1, (PBYTE)&info, sizeof (info));
  417.     (void)DosClose (handle);
  418.     if (host == OS)
  419.     {
  420.         if (DosSetFileMode (name, attribute, 0L))
  421.             return -1;
  422.     }
  423.     return code;
  424. }
  425.  
  426. #endif
  427.  
  428. #ifdef UNIX
  429.  
  430. #define SUBS_DEFINED
  431.  
  432. #include <time.h>
  433.  
  434. #ifndef time_t
  435. #define time_t long
  436. #endif
  437.  
  438. extern struct tm *localtime();
  439. extern time_t time();
  440. extern char   *strcpy();
  441. extern voidp  *malloc();
  442.  
  443. FILE *
  444. file_open(name, mode)
  445. char *name;
  446. char *mode;
  447. {
  448.     return fopen(name, mode);
  449. }
  450.  
  451. int
  452. file_read(buf, size,